import { useRef } from 'react'
import { useEffect, useState } from 'react'
import { Link, useHistory } from 'react-router-dom'
import styles from './index.module.scss'

export function NotFound() {
  const [count, SetCount] = useState(5)
  // let dsq = false
  let dsq: any = useRef(0)
  const history = useHistory()

  useEffect(() => {
    dsq.current = setInterval(() => {
      SetCount((count) => count - 1)
    }, 1000)

    return () => {
      // 清除定时器
      clearInterval(dsq.current)
    }
  }, [])

  useEffect(() => {
    if (count === 0) {
      history.replace('/home')
    }
  }, [count, history])
  return (
    <div className={styles.root}>
      <h1>对不起，您访问的页面不存在~</h1>
      <p>
        将在 {count} 秒后，返回首页（或者：点击立即返回
        <Link to="/home">首页</Link>）
      </p>
    </div>
  )
}

export default NotFound
